package main

import (
	"github.com/ying32/govcl/vcl"
	"github.com/ying32/govcl/vcl/types"
)

// 管理数据源
func (f *MainForm) dbManage() {
	var (
		top        int32 = commonPadding
		dbIndex    int32 = -1
		dbId       *vcl.TLabeledEdit
		dbHost     *vcl.TLabeledEdit
		dbName     *vcl.TLabeledEdit
		dbUsername *vcl.TLabeledEdit
		dbPassword *vcl.TLabeledEdit
	)

	manager := vcl.NewForm(f)
	manager.SetPosition(types.PoMainFormCenter)
	manager.SetWidth(dbWidth)
	manager.SetHeight(dbHeight)
	manager.SetCaption("管理数据源")
	manager.SetBorderStyle(types.BsSingle)

	// 数据源列表
	dbListLabel := vcl.NewLabel(f)
	dbListLabel.SetParent(manager)
	dbListLabel.SetCaption("选择数据源")
	dbListLabel.SetBounds(custom5LabelPadding, top+labelFix, formLabelWidth, formItemHeight)
	dbList := vcl.NewComboBox(f)
	dbList.SetParent(manager)
	dbList.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)
	dbList.SetStyle(types.CsDropDownList)
	dbList.SetOnSelect(func(sender vcl.IObject) {
		dbIndex = dbList.ItemIndex()
		vcl.ThreadSync(func() {
			dbId.SetText(dbConfigs[dbIndex].DBId)
			dbHost.SetText(dbConfigs[dbIndex].DBHost)
			dbName.SetText(dbConfigs[dbIndex].DBName)
			dbUsername.SetText(dbConfigs[dbIndex].DBUsername)
			dbPassword.SetText(dbConfigs[dbIndex].DBPassword)
		})
	})
	for _, dbConfig := range dbConfigs {
		dbList.Items().Add(dbConfig.DBId)
	}

	// 数据源名称,唯一
	top += commonPadding + formItemHeight
	dbId = vcl.NewLabeledEdit(f)
	dbId.SetParent(manager)
	dbId.EditLabel().SetCaption("数据源名称")
	dbId.SetLabelPosition(types.LpLeft)
	dbId.SetLabelSpacing(commonPadding)
	dbId.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)

	// 数据库地址
	top += commonPadding + formItemHeight
	dbHost = vcl.NewLabeledEdit(f)
	dbHost.SetParent(manager)
	dbHost.EditLabel().SetCaption("数据源地址")
	dbHost.SetLabelPosition(types.LpLeft)
	dbHost.SetLabelSpacing(commonPadding)
	dbHost.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)

	// 数据库名称
	top += commonPadding + formItemHeight
	dbName = vcl.NewLabeledEdit(f)
	dbName.SetParent(manager)
	dbName.EditLabel().SetCaption("数据库名称")
	dbName.SetLabelPosition(types.LpLeft)
	dbName.SetLabelSpacing(commonPadding)
	dbName.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)

	// 数据库登陆账户
	top += commonPadding + formItemHeight
	dbUsername = vcl.NewLabeledEdit(f)
	dbUsername.SetParent(manager)
	dbUsername.EditLabel().SetCaption("数据库账号")
	dbUsername.SetLabelPosition(types.LpLeft)
	dbUsername.SetLabelSpacing(commonPadding)
	dbUsername.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)

	// 数据库登陆密码
	top += commonPadding + formItemHeight
	dbPassword = vcl.NewLabeledEdit(f)
	dbPassword.SetParent(manager)
	dbPassword.EditLabel().SetCaption("数据库密码")
	dbPassword.SetLabelPosition(types.LpLeft)
	dbPassword.SetLabelSpacing(commonPadding)
	dbPassword.SetPasswordChar('*')
	dbPassword.SetBounds(formLabelWidth+commonPadding, top, formEditWidth, empty)

	// 操作按钮
	top += formItemHeight
	top += (dbHeight - top - formItemHeight) / 2
	var left int32 = (dbWidth - (3*formButtonWidth + 2*commonPadding)) / 2
	testHandle := vcl.NewButton(f)
	testHandle.SetParent(manager)
	testHandle.SetCaption("测试连接")
	testHandle.SetBounds(left, top, formButtonWidth, formItemHeight)
	testHandle.SetOnClick(func(sender vcl.IObject) {
		testHandle.SetCaption("正在测试")
		testHandle.SetEnabled(false)
		go func() {
			if err := testDBConn(
				DBConfig{
					DBHost:     dbHost.Text(),
					DBName:     dbName.Text(),
					DBUsername: dbUsername.Text(),
					DBPassword: dbPassword.Text(),
				}); err != nil {
				vcl.ShowMessage("数据库连接失败" + lineBreak + err.Error())
				testHandle.SetCaption("测试连接")
				testHandle.SetEnabled(true)
			} else {
				vcl.ShowMessage("数据库连接成功")
				testHandle.SetCaption("测试连接")
				testHandle.SetEnabled(true)
			}
		}()
	})
	saveHandle := vcl.NewButton(f)
	saveHandle.SetParent(manager)
	saveHandle.SetCaption("保存")
	saveHandle.SetBounds(testHandle.Left()+testHandle.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	saveHandle.SetOnClick(func(sender vcl.IObject) {
		if len(dbId.Text()) == 0 {
			vcl.ShowMessage("请输入数据源名称")
			return
		}
		saveHandle.SetCaption("保存中")
		disable(dbId, dbHost, dbName, dbUsername, dbPassword, testHandle)

		exist := false
		for i := range dbConfigs {
			if dbConfigs[i].DBId == dbId.Text() {
				exist = true
				dbConfigs[i].DBHost = dbHost.Text()
				dbConfigs[i].DBName = dbName.Text()
				dbConfigs[i].DBUsername = dbUsername.Text()
				dbConfigs[i].DBPassword = dbPassword.Text()
			}
		}
		if !exist {
			dbConfig := DBConfig{
				DBId:       dbId.Text(),
				DBHost:     dbHost.Text(),
				DBName:     dbName.Text(),
				DBUsername: dbUsername.Text(),
				DBPassword: dbPassword.Text(),
			}
			dbConfigs = append(dbConfigs, dbConfig)
		}
		go func() {
			reloadConfigs(0)
			vcl.ThreadSync(func() {
				vcl.ShowMessage("保存成功")
				manager.Close()
			})
		}()
	})

	deleteHandle := vcl.NewButton(f)
	deleteHandle.SetParent(manager)
	deleteHandle.SetCaption("删除")
	deleteHandle.SetBounds(saveHandle.Left()+saveHandle.Width()+commonPadding, top, formButtonWidth, formItemHeight)
	deleteHandle.SetOnClick(func(sender vcl.IObject) {
		if dbIndex == -1 {
			vcl.ShowMessage("未选中数据库配置")
			return
		}
		dbConfigs = append(dbConfigs[:dbIndex], dbConfigs[dbIndex+1:]...)
		go func() {
			reloadConfigs(0)
			vcl.ThreadSync(func() {
				dbIndex = -1
				clear(dbList, dbId, dbHost, dbName, dbUsername, dbPassword)
				for _, dbConfig := range dbConfigs {
					dbList.Items().Add(dbConfig.DBId)
				}
				vcl.ShowMessage("删除成功")
			})
		}()
	})

	manager.ShowModal()
}
